﻿using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class DownloaderHandler : DownloadHandlerScript
{
    public float progress;
    private ulong ContentLength = 0;
    private int DownedLength = 0;

    FileStream fs;

    public DownloaderHandler(string FilePath) : base(new byte[1024])
    {
        fs = new FileStream(FilePath, FileMode.OpenOrCreate);
    }

    protected override bool ReceiveData(byte[] data, int dataLength)
    {
        if (data == null || data.Length < 1)
        {
            return false;
        }
        fs.Write(data, 0, dataLength);
        DownedLength += dataLength;

        progress = (ulong)DownedLength / ContentLength;
        Debug.Log("当前文件进度：" + progress);
        return true;
    }

    protected override void CompleteContent()
    {
        fs.Close();
        fs.Dispose();
    }

    protected override void ReceiveContentLengthHeader(ulong contentLength)
    {
        ContentLength = contentLength;
    }
}